package com.ruoyi.web.controller.creditlife;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.creditlife.domain.BookBorrrowRecord;
import com.ruoyi.creditlife.service.IBookBorrrowRecordService;
import com.ruoyi.creditlife.service.IBookInfoService;
import com.ruoyi.creditlife.service.util.UserCreditScoreLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/creditlife/borrowRecord")
public class BookBorrrowRecordController extends BaseController
{
    @Autowired
    private IBookBorrrowRecordService bookBorrrowRecordService;

    @Autowired
    private IBookInfoService bookInfoService;

    @Autowired
    private UserCreditScoreLevel userCreditScoreLevel;

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @GetMapping("/list")
    public TableDataInfo list(BookBorrrowRecord bookBorrrowRecord) {
        bookBorrrowRecord.setUserId(SecurityUtils.getUserId());
        bookBorrrowRecordService.batchUpdateOverdueRecords();
        startPage();
        List<BookBorrrowRecord> list = bookBorrrowRecordService.selectBookBorrrowRecordList(bookBorrrowRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @Log(title = "用户借书记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookBorrrowRecord bookBorrrowRecord) {
        List<BookBorrrowRecord> list = bookBorrrowRecordService.selectBookBorrrowRecordList(bookBorrrowRecord);
        ExcelUtil<BookBorrrowRecord> util = new ExcelUtil<BookBorrrowRecord>(BookBorrrowRecord.class);
        util.exportExcel(response, list, "用户借书记录数据");
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bookBorrrowRecordService.selectBookBorrrowRecordById(id));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @Log(title = "用户借书请求处理", businessType = BusinessType.INSERT)
    @PostMapping("/{bookId}/{lentTime}")
    public AjaxResult borrowBook(@PathVariable Integer bookId,
                                 @PathVariable Integer lentTime) {
        boolean updateCount = bookInfoService.updateBookNum(Long.valueOf(bookId));
        if (updateCount == false) {
            return AjaxResult.error("库存不足");
        }

        try {
            int insertCount = bookBorrrowRecordService.userBorrrowBookRecord(
                    Long.valueOf(bookId),
                    Long.valueOf(lentTime)
            );
            return toAjax(insertCount);
        } catch (Exception e) {
            return AjaxResult.error("借书失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @Log(title = "用户借书记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookBorrrowRecord bookBorrrowRecord) {
        return toAjax(bookBorrrowRecordService.insertBookBorrrowRecord(bookBorrrowRecord));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @Log(title = "用户借书记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookBorrrowRecord bookBorrrowRecord) {
        return toAjax(bookBorrrowRecordService.updateBookBorrrowRecord(bookBorrrowRecord));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @Log(title = "用户借书记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bookBorrrowRecordService.deleteBookBorrrowRecordByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:borrowRecord')")
    @GetMapping("/CreditDays")
    public AjaxResult getReserveBookLevelDays() {
        int days = userCreditScoreLevel.getReserveBookLevelDays();
        return AjaxResult.success(days);
    }
}